﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ClipperBaseUI.Controls;
using Clipper.BLL;
using Global;
using ClipperBaseUI.Chart;
using System.Collections.ObjectModel;
using Clipper.Model.DataBaseModel;
using Clipper.Model;

namespace Clipper.ClipperWindow
{
    /// <summary>
    /// WinReagentBarcode.xaml 的交互逻辑
    /// </summary>
    public partial class WinResultShow : BaseWindow
    {
        public WinResultShow()
        {
            InitializeComponent();
        }

        Model.DataBaseModel.CupDetectResultInformation mResult = null;

        private void BaseWindow_Loaded(object sender, RoutedEventArgs e)
        {

            mResult = this.Tag as Model.DataBaseModel.CupDetectResultInformation;

            HoleNum.Text = mResult.CupHoleNum.ToString();
            txt_ReagentDetect.Text = mResult.DetectItem;
            if (mResult.DetectResult.Contains("↑") || mResult.DetectResult.Contains("↓") || mResult.DetectResult.Contains("√"))
            {
                txt_ReagentDetectResult.Text = mResult.DetectResult.Substring(0, mResult.DetectResult.IndexOf("-") - 1);
            }
            else
            {
                txt_ReagentDetectResult.Text = mResult.DetectResult;
            }

            txt_detectTime.Text = mResult.DetectTime.ToString();

            //PT计算值

            if (txt_ReagentDetect.Text.Equals("PT"))
            {
                standardCurve = standardBll.GetModel(txt_ReagentDetect.Text.Trim());  //获取对象
                if (standardCurve == null)
                {
                    return;
                }

                Model.DetectItems detectItem = detectBll.GetModel(txt_ReagentDetect.Text.Trim());
                // detectItem.DetectVarietyID == 1 //数据库中1代表凝固法用 对数
                // detectItem.DetectVarietyID == 2 //数据库中1代表凝固法用 实数

                //活性度计算 PT
                string Activitystr = ServiceProvider.Algorithmclass.getActivityValue(standardCurve, txt_ReagentDetectResult.Text.Trim(), detectItem.DetectVarietyID);

                //INR值 及比率 用到参数 常态时间 12s ,ISI 值 假定 1.04
                double ptSecond = 12.0;
                double ptRatio = Convert.ToDouble(txt_ReagentDetectResult.Text) / ptSecond;

                double ptISI = ServiceProvider.pTISIValue;  //1.03
                double INR = Math.Pow(ptRatio, ptISI);

                //比率 = 12/测定值

                //INR = math.pow(比率，ISI）;


                txt_detectResult.Text = " 比率=" + ptRatio.ToString("#.##") + "R    " +
                                        "INR=" + INR.ToString("#.##") + "   " +
                                        "活性度 = " + Activitystr + "%";

                Showchart(mResult.DetectResultList, mResult.DetectresultTimelist);
            }
            else if (txt_ReagentDetect.Text.Equals("DD") || txt_ReagentDetect.Text.Equals("DDTEST")) //计算浓度值
            {
                //for (int x=0;  x< mResult.DetectResultList.Count;x++)
                //{
                //    if (mResult.DetectResultList[x] == 0)
                //    {
                //        mResult.DetectResultList.RemoveAt(x);
                //    }
                //}

                //验证吸光度的变化值 再代入标准曲线求取浓度
                //txt_detectResult.Text = (ServiceProvider.Algorithmclass.GetOD(mResult.DetectResultList, 20)).ToString("#.##");
                //ObservableCollection<int> DDLIST = new ObservableCollection<int>(mResult.DetectResultList.ToArray());

                ObservableCollection<double> DDLIST = new ObservableCollection<double>();
                //mResult.DetectResultList = GetMedienSmoothOptData(mResult.DetectResultList, 2, 2);   //中值平滑点
                //mResult.DetectResultList = GetShiftAveSmoothOptData(mResult.DetectResultList, 2, 2); //平均值平滑
                for (int i = 0; i < mResult.DetectResultList.Count; i++)
                {

                    DDLIST.Add(Math.Round(Math.Log10((double)mResult.DetectResultList[0] / (double)mResult.DetectResultList[i]) * 10000.0f, 2));
                }


                int ddStart = 400;
                int ddEnd = 600;
                txt_detectResult.Text = (DDLIST[ddEnd] - DDLIST[ddStart]).ToString();
                txt_detectResult.Text += ",," + (mResult.DetectresultTimelist[ddEnd] - mResult.DetectresultTimelist[ddStart]).ToString();

                double myod = ((double)(DDLIST[ddEnd] - DDLIST[ddStart]) /
                    (double)((mResult.DetectresultTimelist[ddEnd] - mResult.DetectresultTimelist[ddStart]) / 1000.0f));

                txt_detectResult.Text += " OD值=" + myod.ToString("#.##");
                standardCurve = standardBll.GetModel(txt_ReagentDetect.Text.Trim());  //获取对象
                try
                {
                    //if (standardCurve == null)
                    //{
                    //    return;
                    //}

                    Model.DetectItems detectItem = detectBll.GetModel(txt_ReagentDetect.Text.Trim());
                    // detectItem.DetectVarietyID == 1 //数据库中1代表凝固法用 对数
                    //detectItem.DetectVarietyID == 2; //数据库中1代表凝固法用 实数

                    //DD 浓度值
                    txt_detectResult.Text += "浓度值=" + ServiceProvider.Algorithmclass.getActivityValue(standardCurve, myod.ToString(), detectItem.DetectVarietyID);
                }
                catch
                {
                }
                Showchart(mResult.DetectResultList, mResult.DetectresultTimelist);

            }
            else if (txt_ReagentDetect.Text.Equals("FIB")) //计算浓度值
            {
                //验证吸光度的变化值 再代入标准曲线求取浓度
                //txt_detectResult.Text = (ServiceProvider.Algorithmclass.GetOD(mResult.DetectResultList, 20)).ToString("#.##");
                //int ddStart = 800;
                //int ddEnd = 1200;
                //txt_detectResult.Text = (mResult.DetectResultList[ddEnd] - mResult.DetectResultList[ddStart]).ToString();
                //txt_detectResult.Text += ",," + (mResult.DetectresultTimelist[ddEnd] - mResult.DetectresultTimelist[ddStart]).ToString();

                //double myod = ((double)(mResult.DetectResultList[ddEnd] - mResult.DetectResultList[ddStart]) /
                //    (double)((mResult.DetectresultTimelist[ddEnd] - mResult.DetectresultTimelist[ddStart]) / 1000.0f));

                //txt_detectResult.Text += " OD值=" + myod.ToString("#.##");
                standardCurve = standardBll.GetModel(txt_ReagentDetect.Text.Trim());  //获取对象
                try
                {
                    //if (standardCurve == null)
                    //{
                    //    return;
                    //}

                    Model.DetectItems detectItem = detectBll.GetModel(txt_ReagentDetect.Text.Trim());
                    // detectItem.DetectVarietyID == 1 //数据库中1代表凝固法用 对数
                    //detectItem.DetectVarietyID == 2; //数据库中1代表凝固法用 实数

                    //DD 浓度值
                    txt_detectResult.Text += "浓度值=" + ServiceProvider.Algorithmclass.getActivityValue(standardCurve, txt_ReagentDetectResult.Text.Trim(), detectItem.DetectVarietyID);
                }
                catch
                {

                }
                Showchart(mResult.DetectResultList, mResult.DetectresultTimelist);
            }
            else
            {
                Showchart(mResult.DetectResultList, mResult.DetectresultTimelist);
            }



            //ObservableCollection<int> DetectResultList2 = new ObservableCollection<int>(mResult.DetectResultList.ToArray());
            //ObservableCollection<int> DetectresultTimelist2 = new ObservableCollection<int>(mResult.DetectresultTimelist.ToArray());

            //chartCurve.AxisX = new Axis(chartCurve, AxisTypeName.X);
            //chartCurve.AxisY = new Axis(chartCurve, AxisTypeName.Y);
            //chartCurve.AxisX.MaxValue = DetectresultTimelist2.Max() / 1000.0f;
            //chartCurve.AxisX.MinValue = DetectresultTimelist2.Min() / 1000.0f;
            //chartCurve.AxisX.StepCount = 10;
            //chartCurve.AxisY.MaxValue = DetectResultList2.Max();
            //chartCurve.AxisY.MinValue = DetectResultList2.Min();
            //chartCurve.AxisY.StepCount = 10;
            //chartCurve.CreateCoordinate();

            //Series series = new Series("series1", chartCurve);
            //chartCurve.SeriesCollection.Add(series);

            ////DetectResultList2 = GetMedienSmoothOptData(DetectResultList2, 2, 2);  //重新平滑曲线
            ////DetectResultList2 = GetShiftAveSmoothOptData(DetectResultList2, 2, 2);

            //ObservableCollection<Point> xyPoint = new ObservableCollection<Point>();
            //double[] yout = new double[DetectResultList2.Count];

            //for (int i = 0; i < DetectResultList2.Count; i++)
            //{
            //    Point temp = new Point();
            //    temp.X = DetectresultTimelist2[i] / 1000.0f;
            //    temp.Y = DetectResultList2[i] - chartCurve.AxisY.MinValue;

            //    xyPoint.Add(temp);
            //}

            //Array youtArray = yout;

            //chartCurve.SeriesCollection[0].ShowData(xyPoint);
        }
        public void Showchart(ObservableCollection<int> DetectResultList2, ObservableCollection<int> DetectresultTimelist2)
        {
            //ObservableCollection<int> DetectResultList2 = new ObservableCollection<int>(mResult.DetectResultList.ToArray());
            //ObservableCollection<int> DetectresultTimelist2 = new ObservableCollection<int>(mResult.DetectresultTimelist.ToArray());

            chartCurve.AxisX = new Axis(chartCurve, AxisTypeName.X);
            chartCurve.AxisY = new Axis(chartCurve, AxisTypeName.Y);
            chartCurve.AxisX.MaxValue = DetectresultTimelist2.Max() / 1000.0f;
            chartCurve.AxisX.MinValue = DetectresultTimelist2.Min() / 1000.0f;
            chartCurve.AxisX.StepCount = 10;
            chartCurve.AxisY.MaxValue = DetectResultList2.Max();
            chartCurve.AxisY.MinValue = DetectResultList2.Min();
            chartCurve.AxisY.StepCount = 10;
            chartCurve.CreateCoordinate();

            Series series = new Series("series1", chartCurve);
            chartCurve.SeriesCollection.Add(series);

            //DetectResultList2 = GetMedienSmoothOptData(DetectResultList2, 2, 2);  //重新平滑曲线
            //DetectResultList2 = GetShiftAveSmoothOptData(DetectResultList2, 2, 2);

            ObservableCollection<Point> xyPoint = new ObservableCollection<Point>();
            double[] yout = new double[DetectResultList2.Count];

            for (int i = 0; i < DetectResultList2.Count; i++)
            {
                Point temp = new Point();
                temp.X = DetectresultTimelist2[i] / 1000.0f;
                temp.Y = DetectResultList2[i] - chartCurve.AxisY.MinValue;

                xyPoint.Add(temp);
            }

            Array youtArray = yout;

            chartCurve.SeriesCollection[0].ShowData(xyPoint);
        }
        public void Showchart(ObservableCollection<double> DetectResultList2, ObservableCollection<int> DetectresultTimelist2)
        {
            //ObservableCollection<int> DetectResultList2 = new ObservableCollection<int>(mResult.DetectResultList.ToArray());
            //ObservableCollection<int> DetectresultTimelist2 = new ObservableCollection<int>(mResult.DetectresultTimelist.ToArray());

            chartCurve.AxisX = new Axis(chartCurve, AxisTypeName.X);
            chartCurve.AxisY = new Axis(chartCurve, AxisTypeName.Y);
            chartCurve.AxisX.MaxValue = DetectresultTimelist2.Max() / 1000.0f;
            chartCurve.AxisX.MinValue = DetectresultTimelist2.Min() / 1000.0f;
            chartCurve.AxisX.StepCount = 10;
            chartCurve.AxisY.MaxValue = DetectResultList2.Max();
            chartCurve.AxisY.MinValue = DetectResultList2.Min();
            chartCurve.AxisY.StepCount = 10;
            chartCurve.CreateCoordinate();

            Series series = new Series("series1", chartCurve);
            chartCurve.SeriesCollection.Add(series);

            //DetectResultList2 = GetMedienSmoothOptData(DetectResultList2, 2, 2);  //重新平滑曲线
            //DetectResultList2 = GetShiftAveSmoothOptData(DetectResultList2, 2, 2);

            ObservableCollection<Point> xyPoint = new ObservableCollection<Point>();
            double[] yout = new double[DetectResultList2.Count];

            for (int i = 0; i < DetectResultList2.Count; i++)
            {
                Point temp = new Point();
                temp.X = DetectresultTimelist2[i] / 1000.0f;
                temp.Y = DetectResultList2[i] - chartCurve.AxisY.MinValue;

                xyPoint.Add(temp);
            }

            Array youtArray = yout;

            chartCurve.SeriesCollection[0].ShowData(xyPoint);
        }
        public static ObservableCollection<int> GetMedienSmoothOptData(ObservableCollection<int> optData, int before_p, int after_p)
        {
            ObservableCollection<int> numArray = new ObservableCollection<int>();
            int index = 0;
            int num2 = (before_p + 1) + after_p;
            double[] array = new double[num2];
            double num3 = 0.0;
            for (int i = 0; i < optData.Count; i++)
            {
                if (((num2 > 1) && (i >= before_p)) && (i < (optData.Count - after_p)))
                {
                    index = 0;
                    for (int j = i - before_p; j <= (i + after_p); j++)
                    {
                        array[index] = optData[j];
                        index++;
                    }
                    Array.Sort<double>(array);
                    num3 = array[index / 2];
                }
                else
                {
                    num3 = optData[i];
                }
                numArray.Add((int)num3);
            }
            return numArray;
        }

        public static ObservableCollection<int> GetShiftAveSmoothOptData(ObservableCollection<int> optData, int before_p, int after_p)
        {
            ObservableCollection<int> numArray = new ObservableCollection<int>();
            int num = (before_p + 1) + after_p;
            int num2 = 0;
            for (int i = 0; i < optData.Count; i++)
            {
                if (((num > 1) && (i >= before_p)) && (i < (optData.Count - after_p)))
                {
                    num2 = 0;
                    for (int j = i - before_p; j <= (i + after_p); j++)
                    {
                        num2 += optData[j];
                    }
                    numArray.Add((int)(num2 / num));
                }
                else
                {
                    numArray.Add(optData[i]);
                }
            }
            return numArray;
        }
        StandardCurveData standardCurve = null;//标准曲线
        BLL.DataBaseBLL.StandardCurveData standardBll = new BLL.DataBaseBLL.StandardCurveData();  //业务逻辑层
        BLL.DetectItems detectBll = new BLL.DetectItems();

        private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //if (comboBox.SelectedItem.Equals("标准曲线"))
            //{
            standardCurve = standardBll.GetModel(txt_ReagentDetect.Text.Trim());  //获取对象
            if (standardCurve == null)
            {
                return;
            }

            Model.DetectItems detectItem = detectBll.GetModel(txt_ReagentDetect.Text.Trim());
            // detectItem.DetectVarietyID == 1 //数据库中1代表凝固法用 对数
            // detectItem.DetectVarietyID == 2 //数据库中1代表凝固法用 实数

            ServiceProvider.Algorithmclass.getActivityValue(standardCurve, txt_ReagentDetectResult.Text.Trim(), detectItem.DetectVarietyID);

            #region  计算 浓度值
            //List<double> concentrationList = ServiceProvider.Monitor.GetConcentrationList(standardCurve);  //浓度集合            
            //List<double> detectResultList = ServiceProvider.Monitor.GetDetectResultList(standardCurve); //检测结果集合

            //double k = 0.00; //斜率
            //double b = 0.00;
            //if (!txt_ReagentDetectResult.Text.Trim().Equals("") || txt_ReagentDetectResult.Text.Trim() != null)
            //{
            //    double txtValue = Convert.ToDouble(txt_ReagentDetectResult.Text.Trim());

            //    if (detectResultList.Max() < txtValue)
            //    {
            //        k = (detectResultList[detectResultList.Count - 1] - detectResultList[detectResultList.Count - 2]) / (concentrationList[concentrationList.Count - 1] - concentrationList[concentrationList.Count - 2]);
            //        b = detectResultList[detectResultList.Count - 1] - k * concentrationList[concentrationList.Count - 1];
            //        textBlock.Text = ((txtValue - b) / k).ToString("#.##");

            //    }
            //    else if (detectResultList.Min() > txtValue)
            //    {
            //        k = (detectResultList[1] - detectResultList[0]) / (concentrationList[1] - concentrationList[0]);
            //        b = detectResultList[1] - k * concentrationList[1];
            //        textBlock.Text = ((txtValue - b) / k).ToString("#.##");

            //    }
            //    else
            //    {
            //        for (int i = 0; i < detectResultList.Count; i++)
            //        {
            //            if (detectResultList[i] < txtValue && txtValue < detectResultList[i + 1])
            //            {
            //                k = Convert.ToDouble((detectResultList[i + 1] - detectResultList[i]) / (concentrationList[i + 1] - concentrationList[i]));
            //                b = detectResultList[i] - k * concentrationList[i];
            //                textBlock.Text = ((txtValue - b) / k).ToString("#.##");
            //            }
            //        }
            //    }
            //}

            #endregion

            //}
            //else if (comboBox.SelectedItem.Equals("标准曲线2"))
            //{


            //}
        }
    }
}
